/*
 * stream.c
 *
 *  Created on: 2017年4月23日
 *      Author: chenss
 */


#include <stream.h>




int Stream_RecvUntil(Stream_t *stream, char buf[], unsigned int maxSize, char till)
{
    unsigned int i;
    int ret;
    char c;
    bool haveLine = FALSE;
//    rbuf_t *rxRbuf;

//    rxRbuf = & stream->rxRBuf;
    ret = -1;
    for(i = 0; i < (maxSize - 1); i++)
    {
//        if(RingBuffer_Read(rxRbuf, & c) == -1)
        if(Stream_Get(stream, (uint8_t *) & c) != 0)
        {
            break;
        }

        if(c == till)
        {
            ret = 0;
            haveLine = TRUE;
            break;
        }

        buf[i] = c;
    }

    buf[i] = '\0';

    // 清除多余的字符
    if((! haveLine) && (i == (maxSize - 1)))
    {
        while(1)
        {
            if(Stream_Get(stream, (uint8_t *) & c) != 0)
            {
                break;
            }

            if(c == till)
            {
                break;
            }
        }
    }

    return ret;
}
